var app = getApp()
var myVar_detail = null
Page({
  data: {
    order_id:0,
    orderinfo:{}
  },
  onHide: function () {
    clearTimeout(myVar_detail)
  },
  onUnload: function () {
    clearTimeout(myVar_detail)
  },
  getDateText(string) {
    var date = new Date(string);
    return (date.getMonth() + 1) + "月" + date.getDate() + "日"
  },
  onLoad: function (options) {
    clearTimeout(myVar_detail)
    let self = this
    if(options && options.order_id){
      self.setData({
        order_id:options.order_id
      })
    }
  },
  onShow(){
    this.loadData()
    var pages = getCurrentPages();
    var prev = pages[pages.length-2]; //prev
    prev.setData({
      isInnerBack:true
    })
  },
  loadData(){
    let self = this
    app.request({
      url: '/home/order/getInfo',
      method: 'post',
      data: {
        order_id: self.data.order_id
      },
      success: (res) => {
        res.result.checkin_time = self.getDateText(res.result.checkin_time)
        res.result.checkout_time = self.getDateText(res.result.checkout_time)
        self.setData({
          orderinfo: res.result
        })
        if (self.data.orderinfo.down_time > 0) {
          self.listcountdown(self, self.data.orderinfo.down_time)
        }
      }
    })
  },
  //倒计时方法
  listcountdown(that, end_time) {
    let currentInfo = this.data.orderinfo
    currentInfo.endtime = this.formatSeconds(end_time);
    if (end_time <= 0) {
      clearTimeout(myVar_detail)
      that.loadData()
      return;
      currentInfo.endtime = "已结束"
    }
    that.setData({
      orderinfo: currentInfo
    })
    myVar_detail = setTimeout(function () {
      that.listcountdown(that, end_time - 1);
    }, 1000)
  },
  formatSeconds(value) {
    var theTime = parseInt(value);// 秒 
    var theTime1 = 0;// 分 
    var theTime2 = 0;// 小时 
    // alert(theTime); 
    if (theTime > 60) {
      theTime1 = parseInt(theTime / 60);
      theTime = parseInt(theTime % 60);
      // alert(theTime1+"-"+theTime); 
      if (theTime1 > 60) {
        theTime2 = parseInt(theTime1 / 60);
        theTime1 = parseInt(theTime1 % 60);
      }
    }
    var result = "" + parseInt(theTime) + "秒";
    if (theTime1 > 0) {
      result = "" + parseInt(theTime1) + "分" + result;
    }
    if (theTime2 > 0) {
      result = "" + parseInt(theTime2) + "小时" + result;
    }
    return result;
  }
})